<?php

namespace yan\sortname;
use Overtrue\Pinyin\Pinyin;
/**
 * This is just an example.
 */
class SortName{
	public static $pinyin;
	public static function toSort($a, $b){
		$a1 = self::getFirstCharter($a);
        $b1 = self::getFirstCharter($b);
        return $a1 > $b1 ? 1 : -1;
	}
	public static function getFirstCharter($str){
		if (empty($str)) {
            return '';
        }
        $fchar = ord($str[0]);
        if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str[0]);
        $fstr = mb_substr($str, 0, 1);
        $fstr = self::getPinyin()->convert($fstr);
        $fchar = !empty($fstr['0']) ? $fstr['0'][0] : 'z';
        return strtoupper($fchar);
	}
	public static function getPinyin(){
		if(empty(self::$pinyin)){
			self::$pinyin = new Pinyin();
		}
		return self::$pinyin;
	}
}